# coding=utf-8

classes = [
    {"name": "abyss", "age": 25, "grade": "A"},
    {"name": "kin", "age": 23, "grade": "B"},
    {"name": "tian", "age": 24, "grade": "B"},
    {"name": "hun", "age": 26, "grade": "A"},
    {"name": "exe", "age": 21, "grade": "A"}
]


# 第一题
def foo(x):
    return x['age']


print(sorted(classes, key=foo, reverse=False))
# [{'name': 'exe', 'age': 21, 'grade': 'A'}, {'name': 'kin', 'age': 23, 'grade': 'B'}, {'name': 'tian', 'age': 24, 'grade': 'B'}, {'name': 'abyss', 'age': 25, 'grade': 'A'}, {'name': 'hun', 'age': 26, 'grade': 'A'}]

print(sorted(classes, key=lambda x: x['age'], reverse=True))


# [{'name': 'hun', 'age': 26, 'grade': 'A'}, {'name': 'abyss', 'age': 25, 'grade': 'A'}, {'name': 'tian', 'age': 24, 'grade': 'B'}, {'name': 'kin', 'age': 23, 'grade': 'B'}, {'name': 'exe', 'age': 21, 'grade': 'A'}]

# 第二题
def foo1(f):
    d = {}
    d['age'] = f['age'] + 1
    f.update(d)
    return f


classes1 = list(map(foo1, classes))
print(classes1)
# [{'name': 'abyss', 'age': 26, 'grade': 'A'}, {'name': 'kin', 'age': 24, 'grade': 'B'}, {'name': 'tian', 'age': 25, 'grade': 'B'}, {'name': 'hun', 'age': 27, 'grade': 'A'}, {'name': 'exe', 'age': 22, 'grade': 'A'}]


# 第三题
print('！！！！！！！！！！！第三题！！！！！！！！')


def foo2(g):
    return g['grade'] == 'A'


print(list(filter(foo2, classes)))
# [{'name': 'abyss', 'age': 26, 'grade': 'A'}, {'name': 'hun', 'age': 27, 'grade': 'A'}, {'name': 'exe', 'age': 22, 'grade': 'A'}]

print(list(filter(lambda t: t['grade'] != 'A', classes)))


# [{'name': 'kin', 'age': 24, 'grade': 'B'}, {'name': 'tian', 'age': 25, 'grade': 'B'}]


# 第四题
def foo2(n):
    if n <= 1:
        return n
    else:
        return (foo2(n - 1) + foo2(n - 2))


int_num = 10
for i in range(int_num):
    print(foo2(i))

"""
0
1
1
2
3
5
8
13
21
34
"""
